/**
 *
 */
package org.insia.smartposse.manager;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.insia.smartposse.form.JobForm;
import org.insia.smartposse.manager.iface.IManager;
import org.insia.smartposse.mapping.JobProject;
import org.insia.smartposse.mapping.dao.JobProjectDAO;

/**
 * @author cmougel
 *
 */
public class JobProjectManager implements IManager {

	private final JobProjectDAO _dao = new JobProjectDAO();
	private final JobProject _jobProject;

	public JobProjectManager(){
		_jobProject = new JobProject();
	}

	public JobProjectManager(JobProject jobProject) {
		_jobProject = jobProject;
	}

	public JobProjectManager(Integer id) {
		_jobProject = _dao.load(id, _dao.getSession());
	}

	/**
	 * Construtor for save the Form in Project Manager
	 *
	 * @param form
	 */
	public JobProjectManager(JobForm form) {
		this();
		setProject(new ProjectManager(form.getProjectId()));
		setSkill(new SkillManager(form.getSkill()));
		setAuction(form.getAuction());
	}

	/**
	 * Define the Skill to this JobProject
	 *
	 * @param jobSkill
	 */
	public void setSkill(SkillManager jobSkill){
		_jobProject.setSkill(jobSkill.getSkill());
	}

	/**
	 *
	 * @return The Skill for this Job
	 */
	public SkillManager getSkill(){
		return new SkillManager(_jobProject.getSkill());
	}

	/**
	 * Define the project for this JobProject
	 *
	 * @param project
	 */
	public void setProject(ProjectManager project){
		_jobProject.setProject(project.getProject());
	}

	/**
	 * @return A ProjectManager for the Project of this JobProject
	 */
	public ProjectManager getProject(){
		return new ProjectManager(_jobProject.getProject(), _dao.getSession());
	}

	/* (non-Javadoc)
	 * @see org.insia.smartposse.manager.iface.IManager#save()
	 */
	public void save() {
		checkComplete();
		_dao.saveOrUpdate(_jobProject);

	}

	/**
	 * made Complete the Project if you add a manager
	 */
	private void checkComplete() {
		if (null == getFreelance()){
			ProjectManager p = getProject();
			p.setComplete(false);
			p.save();
		}
	}

	/**
	 * Save  with the session define
	 *
	 * @param session
	 */
	public void save(Session session) {
		checkComplete();
		Transaction t = session.beginTransaction();
		_dao.saveOrUpdate(_jobProject, session);
		t.commit();

	}

	/**
	 *
	 * @return the JobProject save into
	 */
	public JobProject getJobProject() {
		return _jobProject;
	}

	public Integer getId() {
		return _jobProject.getId();
	}

	/**
	 *
	 * @return The Manager of Project for this Job
	 */
	public UserManager getManager() {
		return new UserManager(_jobProject.getProject().getManagerId());
	}

	/**
	 *
	 * @return The salary for this Job
	 */
	public double getSalary(){
		return _jobProject.getSalary();
	}

	/**
	 * Define the Salary for this Job
	 * @param salary
	 */
	public void setSalary(Integer salary){
		_jobProject.setSalary(salary);
	}

	/**
	 *
	 * @return if this Job Accept Auction or not
	 */
	public Boolean isAuction(){
		return _jobProject.isAuction();
	}

	/**
	 * Alias of isAuction for use it in DisplayTag
	 *
	 * @return
	 */
	public Boolean getAuction(){
		return isAuction();
	}

	/**
	 * Define the Auction accept or not
	 * @param auction
	 */
	public void setAuction(Boolean auction){
		_jobProject.setAuction(auction);
	}

	/**
	 * Define the Freelance for this Job
	 *
	 * @param freelance
	 */
	public void setFreelance(UserManager freelance) {
		if (freelance.isFreelance()){
			_jobProject.setFreelance(freelance.getUser());
			save(_dao.getSession());
			// Check if the Project is now complete or Not.
			if (0 == _dao.nbJobNotCompleteProjectSameProject(getProject(), _dao.getSession())){
				// Is complete and define complete
				ProjectManager pm = getProject();
				pm.setComplete(true);
				pm.save();
				System.out.println("complete");
			}
		} else {
			throw new HibernateException("the User must be a Freelance");
		}
	}

	/**
	 *
	 * @return the freelance for this Job
	 */
	public UserManager getFreelance(){
		if (null == _jobProject.getFreelance()){
			return null;
		} else {
			return new UserManager(_jobProject.getFreelance());
		}
	}
}
